import os
from funasr import AutoModel

asr_model = AutoModel(
    model="paraformer-zh", model_revision="v2.0.4",
    vad_model="fsmn-vad", vad_model_revision="v2.0.4",
    punc_model="ct-punc-c", punc_model_revision="v2.0.4",
    disable_update=True
)

async def wav_to_text(file_url, delete_after=False):
    if not os.path.exists(file_url):
        print(f"File not found: {file_url}")
        return ''

    try:
        param_dict = {"sentence_timestamp": False}
        res = asr_model.generate(input=file_url, is_final=True, **param_dict)
        if res and 'text' in res[0]:
            print(file_url, res[0]['text'])
            return res[0]['text']
        return ''
    except Exception as e:
        print(f"Error during model.generate: {e}")
        return ''
    finally:
        if delete_after:
            try:
                os.remove(file_url)
            except Exception as e:
                print(f"Failed to delete file: {e}")

# if __name__ == '__main__':
#     wav_to_text(r"D:\v4\爱莉希雅\reference_audios\中文\emotions\【默认】由此点亮名为未来的奇迹。.wav")
